package com.lebang.controller.api.earned;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Require;
import com.lebang.common.ValidCode;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.PointEarnedFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.LuckDrawService;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: hwq
 * @Date: 2020/3/24 20:07
 * @Version 1.0
 */
@Api("金币赚")
@RestController
@RequestMapping("/app/ear/point")
public class PointEarnedApi {
    @Autowired
    private PointEarnedFeign pointEarnedFeign;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNoticeService memberNoticeService;
    @Autowired
    private LuckDrawService luckDrawService;
    @Autowired
    private MemberNewTaskService memberNewTaskService;
    @Require
    @PostMapping("/info")
    public JsonData info(HttpServletRequest request) {
        RequestUserInfo userInfo=new RequestUserInfo(request);
        return pointEarnedFeign.info(userInfo);
    }

    @Require
    @PostMapping("/pull")
    public JsonData pull(HttpServletRequest request,  Integer goldId) {
        if(goldId==null){
            return Callback.fail("200", "非法参数");
        }
        RequestUserInfo userInfo=new RequestUserInfo(request);
        RequestHeaders headers=new RequestHeaders(request);
        JsonData data=pointEarnedFeign.pull(userInfo,goldId);
        if(data.isSuccess()){
            JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(data.getData()));
            int point=jsonObject.getIntValue("point");
            pointService.addPoint(userInfo.getMemberId(),point, TaskCategory.POINT_EARN,headers,"");
            memberNoticeService.addNoticeAndPush("金币赚","金币赚奖励"+point,headers.getUserid(),headers);
            memberNewTaskService.addRecord(8890,headers.getUserid());
            return Callback.success("success", "领取成功!",jsonObject);
        }else {
            return data;
        }

       // return new Response<Integer>().fail("success", "领取成功~",config.getPoint());
    }
    @Require
    @PostMapping("/double")
    @ValidCode
    public JsonData pullDouble(HttpServletRequest request,  Long recordId) {
        if(recordId==null){
            return Callback.fail("200", "非法参数");
        }
        RequestUserInfo userInfo=new RequestUserInfo(request);
        RequestHeaders headers=new RequestHeaders(request);
        Result<Integer> result=pointEarnedFeign.pullDouble(userInfo,recordId);
        if(result.isSuccess()){
            pointService.addPoint(userInfo.getMemberId(),result.getData(),
                    TaskCategory.WALK_EARNS,headers,"翻倍奖励:"+result.getData()+"金币");
            memberNoticeService.addNoticeAndPush("金币赚","金币赚翻倍奖励:"+result.getData()+"金币",headers.getUserid(),headers);
            return Callback.success(result.getStatus(), result.getMessage(),result.getData());
        }else {
            return Callback.fail(result.getStatus(), result.getMessage());
        }
    }
    @Require
    @PostMapping("/cool")
    public JsonData cool(HttpServletRequest request, @RequestParam("goldId") Integer goldId) {
        RequestUserInfo userInfo=new RequestUserInfo(request);
       return pointEarnedFeign.cool(userInfo,goldId);
        // return new Response<Integer>().fail("success", "领取成功~",config.getPoint());
    }
    @Require
    @PostMapping("/ad")
    public JsonData ad(HttpServletRequest request) {
        RequestHeaders headers=new RequestHeaders(request);
        return Callback.success("200", "200",luckDrawService.selectAdByBrand(headers.getBrand(), Integer.parseInt(headers.getVersion())).getKey());
    }

    public static void main(String[] args) {
        System.out.println(13);
    }
}
